package com.ktgj.service.controller;

import com.alibaba.fastjson.JSONArray;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.utils.StringUtils;
import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.customer.api.domain.EmployeeApi;
import com.ktgj.service.domain.*;
import com.ktgj.service.domain.vo.TmpViphallVo;
import com.ktgj.service.domain.vo.VipHallVipChannelPrice;
import com.ktgj.service.domain.vo.ViphallPatamter;
import com.ktgj.service.service.IViphallService;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 贵宾厅Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/viphall")
public class ViphallController extends BaseController
{
    @Autowired
    private IViphallService viphallService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询贵宾厅列表
     */
    //PreAuthorize(hasPermi = "service:viphall:list")
    @GetMapping("/list")
    public TableDataInfo list(Viphall viphall)
    {
        startPage();
        List<Viphall> list = viphallService.selectViphallList(viphall);
        return getDataTable(list);
    }

    /**
     * 导出贵宾厅列表
     */
    //@PreAuthorize(hasPermi = "service:viphall:export")
    @Log(title = "贵宾厅 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Viphall viphall) throws IOException
    {
        List<Viphall> list = viphallService.selectViphallList(viphall);
        ExcelUtil<Viphall> util = new ExcelUtil<Viphall>(Viphall.class);
        util.exportExcel(response, list, "贵宾厅 数据");
    }

    /**
     * 获取贵宾厅详细信息
     */
    //@PreAuthorize(hasPermi = "service:viphall:query")
    @GetMapping(value = "/{viphallId}")
    public AjaxResult getInfo(@PathVariable("viphallId") Long viphallId)
    {
        return AjaxResult.success(viphallService.selectViphallById(viphallId));
    }

    /**
     * 新增贵宾厅
     */
    //@PreAuthorize(hasPermi = "service:viphall:add")
    @Log(title = "贵宾厅 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Viphall viphall)
    {
        return toAjax(viphallService.insertViphall(viphall));
    }

    /**
     * 修改贵宾厅
     */
    //@PreAuthorize(hasPermi = "service:viphall:edit")
    @Log(title = "贵宾厅 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Viphall viphall)
    {
        return toAjax(viphallService.updateViphall(viphall));
    }

    /**
     * 删除贵宾厅
     */
    //@PreAuthorize(hasPermi = "service:viphall:remove")
    @Log(title = "贵宾厅 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{viphallId}")
    public AjaxResult remove(@PathVariable Long viphallId)
    {
        return toAjax(viphallService.deleteViphallById(viphallId));
    }


    /**
     * 贵宾厅会员积分消费金额维护
     */
    @PostMapping("/vipHallTotal")
    public AjaxResult vipHallTotal(Map<String, Object> resultMap) {
        return toAjax(viphallService.conVipHallTotal(resultMap));
    }

    /**
     * 新增贵宾厅价格维护
     * */
    @Log(title = "新增贵宾厅价格维护	", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addContractLinkViphall")
    public AjaxResult addContractLinkViphall(@RequestBody ContractLinkViphall contractLinkViphall)
    {
        if(viphallService.addContractLinkViphall(contractLinkViphall)){
            return AjaxResult.success("贵宾厅价格保存成功！");
        }else{
            return AjaxResult.error("贵宾厅价格保存失败！");
        }
    }

    /**
     * 贵宾厅价格维护
     * */
    @PostMapping("/saveOrDelVipHallPrice")
    public AjaxResult saveOrDelVipHallPrice(@RequestBody List<ContractLinkViphallPrice> viphallPrice){
        boolean flag = viphallService.saveOrDelVipHallPrice(viphallPrice);
        return toAjax(flag);
    }

    /**
     * 根据地址定位查询贵宾厅
     * @param viphallVo
     * @return
     */
    @GetMapping("/queryAddress")
    public AjaxResult queryAddress(ViphallVo viphallVo) {
        return AjaxResult.success(viphallService.queryAddress(viphallVo));
    }

    /**
     * 通过id查询标签名称
     */
    @GetMapping("/linkIds")
    public AjaxResult getInfoToId(Long viphallId){
        return AjaxResult.success(viphallService.selectKtserviceViphallTagByIdToTagName(viphallId));
    }

    /**
     * 根据贵宾厅ID查询贵宾厅设施
     */
    @GetMapping("/selectAuxtype")
    public AjaxResult selectKtserviceViphalllAuxtypeByAuxtype(Long viphallId){
        return AjaxResult.success(viphallService.selectKtserviceViphalllAuxtypeByAuxtype(viphallId));
    }

    /**
     * 根据贵宾厅ID展示贵宾厅服务详情页
     * @param viphallId
     * @return
     */
    @GetMapping("/selectContraceViphall")
    public AjaxResult selectContraceViphall(Long viphallId){
        return AjaxResult.success(viphallService.selectContraceViphall(viphallId));
    }

    public AjaxResult viphallShare() {
        return null;
    }

    /**
     * 通过当前选择的机场三字码推荐贵宾厅
     * @param maps
     * @return
     */
    @PostMapping("/queryAirportCode")
    public AjaxResult queryAirportCode(@RequestBody Map<String, Object> maps) {
        String airportCode = String.valueOf(maps.get("airportCode"));
        List<Map<String, Object>> mapList = viphallService.queryAirportCode(airportCode);
        return AjaxResult.success().put("data", mapList);
    }

    /**
     * 通过当前选择的机场三字码推荐贵宾厅服务列表
     * @param airportCode
     * @return
     */
    @GetMapping("/queryAirportCodeList")
    public TableDataInfo queryAirportCodeList(String airportCode) {
        startPage();
        List<ViphallList> list = viphallService.queryAirportCodeList(airportCode);
        return getDataTable(list);
    }

    /**
     * 贵宾厅服务详情页
     * @param map
     * @return
     */
    @PostMapping("/queryViphallId")
    public AjaxResult queryViphallId(@RequestBody Map<String, Object> map) {
        Long viphallId = Long.valueOf(map.get("viphallId").toString());
        return AjaxResult.success(viphallService.queryViphallId(viphallId));
    }

    //根据贵宾厅ID获取贵宾厅基础数据
    @PostMapping("/getViphallById")
    public AjaxResult getViphallById(@RequestBody Map<String, Object> map)
    {
        if (StringHelper.isNullOrEmpty(map.get("viphallId").toString()))
        {
            return AjaxResult.error("参数传递有误");
        }
        Long viphallId = Long.valueOf(map.get("viphallId").toString());
        return AjaxResult.success(viphallService.getViphallById(viphallId));
    }

    /**
     * 获取所有贵宾厅列表远程调用使用
     * @return
     */
    @GetMapping("/getViphallList")
    public List<Viphall> getViphallList(){
        List<Viphall>  list = viphallService.getAll();
        return list;
    }

    /**
     * 通过当前贵宾厅id获取贵宾厅联系电话
     * @param map
     * @return
     */
    @PostMapping("/selectVipHallPhoneById")
    public AjaxResult selectVipHallPhoneById(@RequestBody Map<String, Object> map) {
        Long viphallId = Long.valueOf(map.get("viphallId").toString());
        return AjaxResult.success(viphallService.selectVipHallPhoneById(viphallId));
    }

    /**
     * 根据机场三字码查询改机场有没有贵宾厅
     * */
    @PostMapping("/selectVipHallVoByAirportCode")
    public boolean selectVipHallVoByAirportCode(@RequestParam("airportCode") String airportCode){
        return viphallService.selectVipHallVoByAirportCode(airportCode);
    }

    /**
     * 常用服务
     * @param request
     * @return
     */
    @PostMapping("/commonServices")
    public AjaxResult commonServices(HttpServletRequest request) {
        String token = request.getHeader("token");
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        List<ViphallCommonServices> list = viphallService.commonServices(customer.getData().getCustomerId());
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询贵宾厅价格
     * @param id
     * @return
     */
    @PostMapping("/selectVipHallPrice")
    public AjaxResult selectVipHallPrice(@RequestParam("id") Long id) {
        VipHallVipChannelPrice price = viphallService.selectVipHallPrice(id);
        return AjaxResult.success(price);
    }

    /**
     * 添加贵宾厅
     * @return
     */
    @PostMapping("/saveVipHall")
    public AjaxResult saveVipHall(@RequestBody Viphall viphall){
        if (viphall == null){
            return AjaxResult.error("缺少必要参数");
        }

        if (viphall.getEmploeeId() == null)
        {
            return AjaxResult.error("员工信息为空！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", viphall.getEmploeeId());
        Employee employee  = remoteCustomerService.employeeNameList(map);
        if (employee != null){
            viphall.setDeptId(employee.getDeptId());
        }
        else {
            return AjaxResult.error("未查询到此员工信息！");
        }
        int i;
        try {
            i = viphallService.saveVipHall(viphall);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("成功");
        }
        else {
            return AjaxResult.error("失败");
        }
    }

    /**
     * 贵宾厅基础列表
     * */
    @PostMapping("/viphallList")
    public TableDataInfo viphallList(@RequestBody TmpViphallVo tmpViphallVo) {
        startPage();
        List<Viphall> viphalls = viphallService.viphallList(tmpViphallVo);
        if (viphalls.size() > 0){
            return getDataTable(viphalls);
        }
        else {
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 删除贵宾厅信息
     * */
    @PostMapping("/delViphallVo")
    public AjaxResult delViphallVo(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i = viphallService.delViphallVo(id);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }
}
